﻿Imports System.Data.OleDb
Public Class DangNhapDAO

    Public Function GetDangNhap(ByVal id As String) As DangNhapDTO
        Dim sql As String = String.Format("select * from TaiKhoan where TenDangNhap='{0}'", id)
        Dim Cn As OleDbConnection = DataProvider.OpenConnect
        Dim da As New OleDbDataAdapter(sql, Cn)
        Dim dt As New DataTable()
        da.Fill(dt)
        Cn.Close()
        Dim user As New DangNhapDTO()
        If dt.Rows.Count > 0 Then
            user.ID = dt.Rows(0)("TenDangNhap").ToString()
            user.PW = dt.Rows(0)("MatKhau").ToString()
            user.MaNV = Integer.Parse(dt.Rows(0)("MaNV").ToString())
        End If
        Return user
    End Function
    Public Function GetTaiKhoan() As DataTable
        Dim sql As String = "select * from TaiKhoan"
        Dim dt As New DataTable
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim da As New OleDbDataAdapter(sql, cn)
        da.Fill(dt)
        cn.Close()
        Return dt
    End Function
    Public Sub AddTaiKhoan(ByVal newTK As DangNhapDTO)
        Dim sql As String = String.Format("insert into TaiKhoan(MaTaiKhoan,TenDangNhap,MatKhau,MaNV,NTNS,DiaChi) values('{0}','{1}','{2}','{3}','{4}','{5}')", _
                                            newTK.MaTaiKhoan, newTK.ID, newTK.PW, newTK.MaNV, newTK.NTNS, newTK.DiaChi)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Vui lòng kiểu tra lại dữ liệu !")
        End Try

        cn.Close()
    End Sub
    Public Sub DelTaiKhoan(ByVal delTK As DangNhapDTO)
        Dim sql As String = String.Format("delete from TaiKhoan where MaTaiKhoan={0}", delTK.MaTaiKhoan)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub
    Public Sub UpdateTaiKhoan(ByVal newTK As DangNhapDTO)
        Dim sql As String = String.Format("update TaiKhoan set TenDangNhap='{0}',MatKhau='{1}',MaNV='{2}',NTNS='{3}',DiaChi='{4}' where MaTaiKhoan={5}", _
                                           newTK.ID, newTK.PW, newTK.MaNV, newTK.NTNS, newTK.DiaChi, newTK.MaTaiKhoan)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Vui lòng kiểu tra lại dữ liệu !")
        End Try
        cn.Close()
    End Sub

End Class
